import 'package:flutter/cupertino.dart';

import '../res/resources.dart';
import '../utils/utils.dart';

class StateLayout extends StatefulWidget {
  final StateType type;
  final String hintText;
  final String img;

  const StateLayout({
    Key key,
    @required this.type,
    this.hintText,
    this.img,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return StateLayoutState();
  }
}

class StateLayoutState extends State<StateLayout> {
  String _img;
  String _hintText;

  @override
  Widget build(BuildContext context) {
    switch (widget.type) {
      case StateType.loading:
        _img = widget.img.isEmpty
            ? Utils.getImgPath("demo/bg_mood_1")
            : widget.img;
        _hintText = widget.hintText.isEmpty ? "加载中" : widget.hintText;
        break;
      case StateType.logic:
        _img = widget.img.isEmpty
            ? Utils.getImgPath("demo/bg_mood_2")
            : widget.img;
        _hintText = widget.hintText.isEmpty ? "逻辑错误" : widget.hintText;
        break;
      case StateType.network:
        _img = widget.img.isEmpty
            ? Utils.getImgPath("demo/bg_mood_3")
            : widget.img;
        _hintText = widget.hintText.isEmpty ? "网络异常" : widget.hintText;
        break;
    }

    return Container(
      width: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          widget.type != StateType.loading
              ? Container(
                  height: 120,
                  width: 120,
                  decoration: BoxDecoration(
                    image: DecorationImage(image: AssetImage(_img)),
                  ),
                )
              : CupertinoActivityIndicator(
                  radius: 18.0,
                ),
          Gaps.vGap16,
          Text(
            widget.hintText ?? _hintText,
            style: TextStyles.textGray16,
          ),
          Gaps.vGap50,
        ],
      ),
    );
  }
}

enum StateType {
  network,
  logic,
  loading,
}
